﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class brandManager : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            CSUtils.CheckSession();
            this.binddata();
            PanelInput.Visible = false;
        }

    }

    private void binddata() {
        GridView1.DataSource = this.getAllBrand();
        GridView1.DataBind();
    }

    private DataSet getAllBrand()
    {
        string sql =
            "select * " +
            "  from cs_brands " +
            "  order by brand_id";
        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        GridView1.PageIndex = e.NewPageIndex;
        this.binddata();
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
        
        if (e.CommandName.Equals("command_edit") ||
            e.CommandName.Equals("command_add")) {

            PanelInput.Visible = true;
            TextBoxCommandType.Text = e.CommandName;
            LabelBrandID.Text = e.CommandArgument.ToString();
            BrandVO vo = CSUtils.getBrand(LabelBrandID.Text);
            TextBoxBrandCode.Text = vo.Code;
            TextBoxBrandName.Text = vo.Name;
            DropDownListStockOutOnly.SelectedValue = vo.StockOutOnly;
        }
        else if (e.CommandName.Equals("command_del"))
        {
            string id = e.CommandArgument.ToString();

            if (this.doDelete(id))
            {
            }
            else
            {
                JScript.Alert("删除失败, 此数据已被其他功能引用!\\n请先删除相关数据!");
            }

            this.binddata();
        }
        else {
            this.cleanPanelInput();
        }               

    }


    private bool doDelete(string id)
    {

        string sqlDel =
            "delete cs_brands " +
            "where brand_id = " + id;

        DBConn myDB = new DBConn();
        int tmp;
        try
        {
            tmp = myDB.Delete(sqlDel);
        }
        catch
        {
            myDB.Close();
            return false;
        }
        myDB.Close();
        return (tmp > 0);

    }

    protected void ButtonSubmit_Click(object sender, EventArgs e) {

        string brandID = LabelBrandID.Text;
        string brandCode = TextBoxBrandCode.Text;
        string brandName = TextBoxBrandName.Text;

        string commandType = TextBoxCommandType.Text;
        BrandVO vo = null;
        if (commandType.Equals("command_edit"))
        {
            //get vo from db
            vo = CSUtils.getBrand(brandID);

            //update there info
            vo.Code = brandCode;
            vo.Name = brandName;            
        }
        else if (commandType.Equals("command_add"))
        {
            if (CSUtils.haveBrand(brandName))
            {
                JScript.Alert("该品牌已经存在, 请不要重复添加!");
                return;
            }

            vo = new BrandVO();
            vo.Code = brandCode;
            vo.Name = brandName;
            vo.Market = "内销";
        }

        if (DropDownListStockOutOnly.SelectedValue == "True") {
            vo.StockOutOnly = "1";
        }else {
            vo.StockOutOnly = "0";
        }

        if (CSUtils.mergeBrand(vo))
        {
            
        }
        else
        {
            JScript.Alert("保存失败！");
        }

        this.binddata();

    }

    protected void LinkButtonAdd_Click(object sender, EventArgs e) {
        PanelInput.Visible = true;
        TextBoxCommandType.Text = "command_add";
        this.cleanPanelInput();

    }

    private void cleanPanelInput() {

        LabelBrandID.Text = "";
        TextBoxBrandCode.Text = "";
        TextBoxBrandName.Text = "";
        
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {        
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    //白包装 不可以修改并且固定ID为1, 销售出货时要用!
        //    DataRowView row = (DataRowView)e.Row.DataItem;
        //    if (row[2].ToString() == "白包装")
        //    {
        //        e.Row.Cells[4].Text = "";
        //    }

        //}

    }

}
